import { register } from 'register-service-worker'
console.log(window.location.host);
if (process.env.NODE_ENV === 'production') {
    register(`${window.location.host}/service-worker.js`, {
        ready() {
            console.log(
                'App is being served from cache by a service worker.\n' +
                'For more details, visit https://goo.gl/AFskqB'
            )
        },
        registered(registration) {
            // 通过定时监测功能。
            setInterval(() => {
                registration.update();
            }, 1000);
            console.log('Service worker has been registered.')
        },
        cached() {
            console.log('Content has been cached for offline use.')
        },
        updatefound() {
            console.log('New content is downloading.')
        },
        updated(registration) {
            console.log('New content is available; please refresh.')
            // 这里已经将最新版下载到本地，询问用户是否立即刷新
            var res = 'confirm("是否立即更新")'
            if (res) {
                window.location.reload();
            }
        },
        offline() {
            console.log('No internet connection found. App is running in offline mode.')
        },
        error(error) {
            console.error('Error during service worker registration:', error)
        }
    })
}